package com.nix.demo.viewgroup.scroll;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.nix.ui.PageControlView;
import com.nix.ui.ScrollLayout;
import com.nix.ui.ScrollLayout.OnScreenChangeListenerDataLoad;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListViewGroupScrollActivity extends Activity {

	private LinkedList<String> data;
	private ScrollLayout mScrollLayout;
	private com.nix.ui.PageControlView pageControl;
	private DataLoading dataLoad;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);


		mScrollLayout = (ScrollLayout)findViewById(R.id.scrollLayout);
		dataLoad = new DataLoading();
		data = new LinkedList<String>();
		for(int i=0;i<10;i++){
			data.add(String.valueOf(i));
		}

		for (int i = 0; i < 4; i++) {
			ListView listView = new ListView(this);
			listView.setAdapter(new SimpleAdapter(this, getData(i), R.layout.item, new String[]{"name"}, new int[]{R.id.textview}));
			listView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
			listView.setDivider(null);
			mScrollLayout.addView(listView);
		}
		//加载分页
		pageControl = (PageControlView) findViewById(R.id.pageControl);
		pageControl.bindScrollViewGroup(mScrollLayout);
		//加载分页数据
		dataLoad.bindScrollViewGroup(mScrollLayout);
	}
	
	private List<? extends Map<String, ?>> getData(int index) {
		ArrayList arrayList = new ArrayList();
		Random random = new Random();
		for(int i=0;i<10+5*index;i++){
			HashMap<String, String> hashMap = new HashMap<String, String>();
			hashMap.put("name", "test... "+index+": "+Math.abs(random.nextInt())%40);
			arrayList.add(hashMap);
		}
		return arrayList;
	}
	

	//分页数据
	class DataLoading {
		private int count;
		public void bindScrollViewGroup(ScrollLayout scrollViewGroup) {
			this.count=scrollViewGroup.getChildCount();
			scrollViewGroup.setOnScreenChangeListenerDataLoad(new OnScreenChangeListenerDataLoad() {
				public void onScreenChange(int currentIndex) {
					// TODO Auto-generated method stub
					generatePageControl(currentIndex);
				}
			});
		}
		
		private void generatePageControl(int currentIndex){
			//如果到最后一页，就加载16条记录
			if(count==currentIndex+1){
				
			}
		}
	}
}


